#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os
import logging
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    # import pymel.core as pm
    # import JTK.jmaya.core as jmc

except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CheckColorSets(CheckBase):
    def __init__(self):
        super(CheckColorSets, self).__init__()
        pass

    def check_color_set(self, d=False):
        mesh_list = cmds.ls(type='mesh', l=1)

        if not mesh_list:
            return []
        loss_colorsets_list = []
        for mesh in mesh_list:
            color_set_name = cmds.getAttr('%s.currentColorSet' % mesh)
            if color_set_name:
                all_color_list = cmds.polyColorSet(mesh, q=1, acs=1)
                if len(all_color_list) > 1:
                    loss_colorsets_list.append(mesh)
                if d:
                    del_color = list(set(all_color_list) - set(cmds.polyColorSet(mesh, q=1, ccs=1)))
                    for cs in del_color:
                        cmds.polyColorSet(mesh, d=1, cs=cs)
            else:
                all_color_list = cmds.polyColorSet(mesh, q=1, acs=1)
                if all_color_list:
                    if len(all_color_list) > 1:
                        loss_colorsets_list.append(mesh)
                    if d:
                        ccs = cmds.polyColorSet(mesh, q=1, ccs=1) or []
                        del_color = list(set(all_color_list) - set(ccs))
                        for cs in del_color:
                            cmds.polyColorSet(mesh, d=1, cs=cs)
        return loss_colorsets_list

    def check(self):

        result_proc = self.check_color_set()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        self.check_color_set(d=True)
        return self.check()